package com.zzsin.gtestreporter.report;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.LinkedList;
import java.util.List;

public class XMLReport {

    private String path = null;
    private String id = "";

    private List<Testsuite> testsuiteList = new LinkedList<Testsuite>();

    public XMLReport(String path){
        this.path = path;
    }

    public int parse(){
        File xmlFile = new File(this.path);
        id = xmlFile.getName();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document d = builder.parse(xmlFile.getAbsoluteFile());
            NodeList testsuiteListNode = d.getElementsByTagName("testsuite");

            for(int i=0; i<testsuiteListNode.getLength(); i++){
                Node node = testsuiteListNode.item(i);

                Testsuite testsuite = new Testsuite(node);
                testsuite.parse();
                testsuiteList.add(testsuite);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Testsuite> getTestsuiteList() {
        return testsuiteList;
    }

    public void setTestsuiteList(List<Testsuite> testsuiteList) {
        this.testsuiteList = testsuiteList;
    }
}
